package com.pachong.android.frameworkbase.customviews;


import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.pachong.android.frameworkbase.R;


/**
 * 一个LinearLayout的扩展, 可以自动添加分割线, 并且控制分割线
 * 分割线默认使用theme中的配置
 *
 * @author Vivi Peng
 * @version 1.0.0
 * @e-mail pengveivei@sina.cn
 * @QQ 897058316
 * @since 15-5-5
 */
public class DividerLinearLayout extends LinearLayout {
    private static final String TAG = DividerLinearLayout.class.getSimpleName();
    private static final String DIVIDER_TAG = "divider_tag";


    public DividerLinearLayout(Context context) {
        this(context, null);
        setupViews(null);
    }


    public DividerLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        setGravity(Gravity.CENTER_HORIZONTAL);
        setupViews(attrs);
    }


    private int mDividerResId;
    private float mDividerHeight;
    private int mDividerMarginLeft;
    private int mDividerMarginRight;
    private int mDividerMarginTop;
    private int mDividerMarginBottom;


    private void setupViews(AttributeSet attrs) {

        if (attrs != null) {
            TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.DividerLinearLayout);
            //Set orientation
            int orientation = a.getInt(R.styleable.DividerLinearLayout_android_orientation, VERTICAL);
            setOrientation(orientation == VERTICAL ? VERTICAL : HORIZONTAL);

            mDividerHeight = a.getDimension(R.styleable.DividerLinearLayout_android_dividerHeight, -1);
            mDividerMarginLeft = (int) a.getDimension(R.styleable.DividerLinearLayout_dividerMarginLeft, 0);
            mDividerMarginRight = (int) a.getDimension(R.styleable.DividerLinearLayout_dividerMarginRight, 0);
            mDividerMarginTop = (int) a.getDimension(R.styleable.DividerLinearLayout_dividerMarginTop, 0);
            mDividerMarginBottom = (int) a.getDimension(R.styleable.DividerLinearLayout_dividerMarginBottom, 0);

            mDividerResId = a.getResourceId(R.styleable.DividerLinearLayout_android_divider, -1);

            a.recycle();
        }

    }


   /* @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        if (mDividerResId > 0) {
            if (getChildCount() > 0) {
                int dividerCount = getChildCount() - 1;
                for (int index = 1; index / 2 < dividerCount; index += 2) {
                    addView(createDivider(), index);
                }

            }
        }
    }*/

    @Override
    public void addView(View child, int index, ViewGroup.LayoutParams params) {
        if (getChildCount() > 0) {
            View divider = createDivider();
            divider.setTag(DIVIDER_TAG);
            super.addView(divider, index, divider.getLayoutParams());
        }
        super.addView(child, index, params);
    }


    /**
     * 创建分割线
     *
     * @return
     */
    private ImageView createDivider() {
        ImageView divider = new ImageView(getContext());
        divider.setImageResource(mDividerResId);
        LayoutParams lp = null;
        if (mDividerHeight > 0f) {
            lp = new LayoutParams(LayoutParams.MATCH_PARENT, (int) mDividerHeight);
        } else {
            lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        }
        lp.setMargins(mDividerMarginLeft, mDividerMarginTop, mDividerMarginRight, mDividerMarginBottom);
        divider.setLayoutParams(lp);
        return divider;
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        hideOrShowDivider();
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    /**
     * 隐藏不可见的View对应的Divider
     */
    private void hideOrShowDivider() {
        if (getChildCount() > 0) {
            for (int i = 0; i < getChildCount(); i++) {
                View childView = getChildAt(i);
                // 查到一个divider, 默认为divider的下一个child肯定存在
                if (childIsDivider(childView)) {
                    View dividerView = childView;
                    View nextChild = getChildAt(++i);
                    // 如果nextChild为GONE, 则同时隐藏divider
                    // 这里不处理INVISIBLE的情况
                    if (nextChild.getVisibility() == View.GONE) {
                        if(dividerView.getVisibility() != View.GONE) {
                            dividerView.setVisibility(View.GONE);
                        }
                    } else {
                        if(dividerView.getVisibility() == View.GONE) {
                            dividerView.setVisibility(View.VISIBLE);
                        }
                    }
                }
            }
        }
    }

    /**
     * 判断是否是分割线
     *
     * @param childView
     * @return
     */
    private boolean childIsDivider(View childView) {
        String tag = (String) childView.getTag();
        return DIVIDER_TAG.equals(tag) ? true : false;
    }

}
